package com.foreverwin.jzymes.eap.Listener;

import com.alibaba.fastjson.JSONObject;
import com.foreverwin.jzymes.common.domain.CarrierScanLog;
import com.foreverwin.jzymes.common.util.CarrierUtil;
import com.foreverwin.jzymes.framework.annotation.MessageLog;
import com.foreverwin.jzymes.framework.event.AppEvent;
import com.foreverwin.jzymes.framework.event.AppEventPublisher;
import com.foreverwin.jzymes.framework.util.ObjectMapperUtil;
import com.foreverwin.jzymes.production.inspect.service.IInspectAdviceService;
import org.apache.activemq.command.ActiveMQTopic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

@Component
public class InspectListener {

    private Logger logger = LoggerFactory.getLogger( InspectListener.class );

    @Autowired
    IInspectAdviceService inspectAdviceService;
    @Autowired
    JmsMessagingTemplate jmsTemplate;
    @Autowired
    AppEventPublisher appEventPublisher;

    @JmsListener( destination = "MES_CARRIER_CHECK_INFRA" )
    @MessageLog
    public void inspectCarrierFromQueue( String carrierScanLogJson ){
        logger.info( "接收到全检信息: " + carrierScanLogJson );
        String site = null;
        String carrier = null;
        String resrce = null;
        try{
            CarrierScanLog carrierScanLog = ObjectMapperUtil.readValueForUnderscore( carrierScanLogJson, CarrierScanLog.class );
            site = carrierScanLog.getSite();
            carrier = carrierScanLog.getCarrier();
            resrce = carrierScanLog.getResrce();
            carrierScanLog.setCarrier( CarrierUtil.buildCarrier( site, carrier ) );
            inspectAdviceService.inspectCarrier( carrierScanLog );

            if( "Y".equals( carrierScanLog.getPass() ) ){
                JSONObject eventData = new JSONObject();
                eventData.put( "SITE", carrierScanLog.getSite() );
                eventData.put( "CARRIER", carrierScanLog.getCarrier() );
                eventData.put( "RESRCE", carrierScanLog.getResrce() );
                AppEvent<JSONObject> appEvent = new AppEvent<>( this );
                appEvent.setName( "CARRIER_CHECK_OUT_EVENT" );
                appEvent.setData( eventData );
                appEventPublisher.publishEvent( appEvent );
            }

            logger.info( "全检信息处理完成: " + carrierScanLogJson );
        }catch ( Exception e ){
            /**保存全检报警记录*/
            inspectAdviceService.saveInspectAlarmLog( site, carrier, resrce, e.getMessage() );

            /**发布报警信息到看板*/
            JSONObject eventData = new JSONObject();
            eventData.put( "SITE", site );
            eventData.put( "RESRCE", resrce );
            AppEvent<JSONObject> appEvent = new AppEvent<>( this );
            appEvent.setName( "INSPECT_ALARM" );
            appEvent.setData( eventData );
            ActiveMQTopic topic = new ActiveMQTopic( "MES_APP_EVENT_INFRA" );
            jmsTemplate.convertAndSend( topic, appEvent );

            logger.error( "全检信息处理失败: " + e.getMessage() + "，全检信息: " + carrierScanLogJson );
        }
    }
}
